package Visual;

import Logica.Cliente;
import Logica.DiaLaboral;
import Logica.Diagnostico;
import Logica.Empleado;
import Logica.Localidad;
import Logica.Taller;
import Logica.Vehiculo;
import Persistencia.exceptions.PreexistingEntityException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JDesktopPane;

public class frmAltaTurno extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual;
    JDesktopPane miEscritorio;
    Cliente unCliente = null;
    Vehiculo unVehiculo = null;
    DefaultListModel modeloTalleres = new DefaultListModel();

    public frmAltaTurno(ControladoraVisual miVisual, JDesktopPane miEscritorio, Cliente unCliente, Vehiculo unVehiculo) {
        initComponents();
        this.setTitle("Alta de Turno");
        this.miVisual = miVisual;
        this.miEscritorio = miEscritorio;
        this.unCliente = unCliente;
        this.unVehiculo = unVehiculo;
        this.txtCliente.setText(unCliente.getNombre() + " " + unCliente.getApellido());
        this.txtVehiculo.setText(unVehiculo.getUnModelo().getUnaMarca().getNombre() + " " + unVehiculo.getUnModelo().getNombre());
        this.txtCliente.setEnabled(false);
        this.txtVehiculo.setEnabled(false);
        this.listaTalleres.setModel(modeloTalleres);
    }

    public Calendar cargarFecha() {
        Calendar fechaTurno = null;
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
        String fechita = cmbFechas.getText();
        try {
            Date fecha = formatter.parse(fechita);
            fechaTurno = Calendar.getInstance();
            fechaTurno.setTime(fecha);
        } catch (ParseException ex) {
            Logger.getLogger(frmAltaTurno.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fechaTurno;
    }

    public void cargarTalleres() {
        List<Taller> talleres = miVisual.dameTalleres();
        for (Taller unTaller : talleres) {
            List<Localidad> zona = unTaller.getZonasCobertura();
            for (Localidad unaLocalidad : zona) {
                if (unCliente.getUnaLocalidad().getCodigo() == unaLocalidad.getCodigo()) {
                    modeloTalleres.addElement(unTaller);
                }
            }
        }
        if (modeloTalleres.isEmpty()) {
            for (Taller unTaller : talleres) {
                modeloTalleres.addElement(unTaller);
            }
        }
        listaTalleres.setModel(modeloTalleres);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel8 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        btnSalir = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtNroTurno = new javax.swing.JTextField();
        txtHora = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtVehiculo = new javax.swing.JTextField();
        btnDiagnostico = new javax.swing.JButton();
        btnAltaTurno = new javax.swing.JButton();
        cmbFechas = new datechooser.beans.DateChooserCombo();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaTalleres = new javax.swing.JList();
        cargarTalleres = new javax.swing.JButton();

        jLabel8.setText("jLabel8");

        jLabel4.setText("Cliente");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("DATOS DEL TURNO");

        jLabel2.setText("Nro de Turno");

        jLabel7.setText("Fecha");

        jLabel9.setText("Hora");

        jLabel10.setText("Vehiculo");

        btnDiagnostico.setText("Realizar Diagnóstico");

        btnAltaTurno.setText("Alta Turno");
        btnAltaTurno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaTurnoActionPerformed(evt);
            }
        });

        jLabel3.setText("Talleres cercanos");

        jScrollPane1.setViewportView(listaTalleres);

        cargarTalleres.setText("Cargar Talleres Cercanos");
        cargarTalleres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargarTalleresActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnAltaTurno)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnSalir))
                            .addComponent(jSeparator3)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtHora)
                                        .addComponent(cmbFechas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(txtNroTurno))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addGap(12, 12, 12))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                                    .addComponent(txtVehiculo)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnDiagnostico))
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(cargarTalleres)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtNroTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(cmbFechas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel10)
                        .addComponent(txtVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cargarTalleres)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnDiagnostico, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAltaTurno))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnAltaTurnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaTurnoActionPerformed
        int nroTurno = Integer.valueOf(txtNroTurno.getText());
        Calendar fechaTurno = this.cargarFecha();
        Time horaTurno = Time.valueOf(txtHora.getText());

        Time duracion = null;
        Time tiempoEstimado = null;
        Empleado unEmpleado = null;
        Diagnostico unDiagnostico = null;
        DiaLaboral unDiaLaboral = null;
        Taller unTaller = null;
        try {
            miVisual.altaTurno(nroTurno, fechaTurno, horaTurno, duracion, tiempoEstimado, unCliente, unEmpleado,
                    unDiagnostico, unVehiculo, unDiaLaboral, unTaller);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaTurno.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaTurno.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnAltaTurnoActionPerformed

    private void cargarTalleresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargarTalleresActionPerformed
        cargarTalleres();
    }//GEN-LAST:event_cargarTalleresActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAltaTurno;
    private javax.swing.JButton btnDiagnostico;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton cargarTalleres;
    private datechooser.beans.DateChooserCombo cmbFechas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JList listaTalleres;
    private javax.swing.JTextField txtCliente;
    private javax.swing.JTextField txtHora;
    private javax.swing.JTextField txtNroTurno;
    private javax.swing.JTextField txtVehiculo;
    // End of variables declaration//GEN-END:variables
}
